package tk.mystudio.web.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KeyValueUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(KeyValueUtil.class);
	
	private KeyValueUtil() {
	}
	
	public static List<KeyValueEntity> getList(String className) {
		List<KeyValueEntity> list = new ArrayList<KeyValueEntity>();
		try {
			Class clazz = Class.forName(className);
			Field fields[] = clazz.getDeclaredFields();
			for(Field field : fields) {
				KeyValueEntity kve = new KeyValueEntity();
				kve.setKey(clazz.getSimpleName()+"."+field.getName());
				kve.setValue(field.get(null).toString());
				list.add(kve);
			}
		} catch (ClassNotFoundException e) {
			logger.error("xxxxxx 未找到常量类{}", className);
		} catch (Exception e) {
			logger.error("xxxxxx 取常量列表时发生异常(class={})", className);
			logger.error("xxxxxx ", e);
		}
		return list;
	}
	
	public static void main(String[] args) {
		for(KeyValueEntity kve : getList("tk.mystudio.constant.AreaLevel")) {
			System.out.println(kve);
		}
	}

}
